<?php
class AutoAction extends AdminAction {

    public function index(){
    	$Data = M('auto'); // 实例化Data数据对象
    	import("@.ORG.Page");// 导入分页类
    	$count = $Data->count();// 查询满足要求的总记录数
    	$Page = new Page($count,20);// 实例化分页类 传入总记录数
    	
    	$nowPage = isset($_GET['p'])?$_GET['p']:1;//当前页码
    	$list = $Data->page($nowPage.','.$Page->listRows)->select();
    	$show = $Page->show();// 分页显示输出
    	$this->assign('page',$show);// 赋值分页输出
    	$this->assign('list',$list);// 赋值数据集
    	$this->display(); // 输出模板
    }
    
    public function model(){
    	$brand = M('auto')->select();
    	$this->assign('brand',$brand);// 赋值数据集
    	
        if(!empty($_GET['brand_id'])){
            $this->assign('brand_id',$map['brand_id']);// 赋值数据集
            $map['brand_id']=$_GET['brand_id'];
        }
    	
    	$Data = M('auto_model'); // 实例化Data数据对象
    	import("@.ORG.Page");// 导入分页类
    	$count = $Data->where($map)->count();// 查询满足要求的总记录数
    	$Page = new Page($count,20);// 实例化分页类 传入总记录数

    	foreach($map as $key=>$val) {
            $Page->parameter   .=   "$key=".urlencode($val).'&';
        }

    	$nowPage = isset($_GET['p'])?$_GET['p']:1;//当前页码
    	$list = $Data->where($map)->page($nowPage.','.$Page->listRows)->select();
    	$show = $Page->show();// 分页显示输出
    	$this->assign('page',$show);// 赋值分页输出
    	$this->assign('list',$list);// 赋值数据集
    	$this->display(); // 输出模板
    }

    //修改
    public function edit_brand(){
    	if($_REQUEST['brand_id']){
    		$pei = M('auto');
    		$data = $pei ->where('brand_id='.$_REQUEST['brand_id'])->find();
    
    		if(empty($data)){
    			$this->error('未找到数据');
    		}
    		$this->assign('data',$data);
    		$this->assign('title',"编辑");
    	}else{
    		$this->assign('title',"添加");
    	}
    	$this->display();
    }
    
    //添加修改
    public function save_brand(){
    	$data = $_POST;
    	//上传图片
    	if (!is_dir($path)) {
    		mkdir($path);
    	}
    	if (count($_FILES) > 0) {
    		$file =(array)uploadFile(250,250);
    		for ($i = 0; $i < count($file); $i++) {
    			$data['brand_logo'] = $path ."m_".$file[$i]['savename'];
    		}
    	}
    	$pei = M('auto');
    	if($data['brand_id']){
    		$res= $pei ->save($data);
    	}else{
    		$res= $pei ->add($data);
    	}
    	 
    	$this->success('成功',U('Auto/index'));
    }
    
    //修改
    public function edit_auto(){
    	$A = M('Auto');
    	$auto = $A->field("brand_id,brand_name")->where($map)->select();
    	$this->assign('auto', $auto);
    	
    	if($_REQUEST['model_id']){

    		$AM = M('auto_model');
    		$data = $AM ->where('model_id='.$_REQUEST['model_id'])->find();
    
    		if(empty($data)){
    			$this->error('未找到数据');
                exit;
    		}

            //获取相关车型
            $map['brand_name']=$data['brand_name'];
            $map['model_name']=$data['model_name'];
            $map['model_year']=$data['model_year'];
            $map['model_pl']=$data['model_pl'];

            $autoList=$AM->where($map)->select();
            $this->assign("autoList",$autoList);

            //获取配件数据
            $PJ=M("peijian");


            //获取该车型匹配配件
            $LJ=M("jl");
            $jilvPP=$LJ->where("model_id=".$data['model_id'])->select();
            foreach($jilvPP as $key=>$value){
                $peijianIdList[$value['type']][]=$value['pj_id'];   //所匹配的配件ID
            }
            $this->assign("peijianIdList",$peijianIdList);


            $jilv=$PJ->where("type_id=2")->select();
            $this->assign("jilv",$jilv);

            $kongqilv=$PJ->where("type_id=3")->select();
            $this->assign("kongqilv",$kongqilv);

            $kongtiaolv=$PJ->where("type_id=5")->select();
            $this->assign("kongtiaolv",$kongtiaolv);

            $huohuasai=$PJ->where("type_id=14")->select();
            $this->assign("huohuasai",$huohuasai);

    		$this->assign('data',$data);
    	}
    	$this->display();
    }

    //添加车型
    public function add_auto(){
        $A = M('Auto');
        $auto = $A->field("brand_id,brand_name")->where($map)->select();
        $this->assign('auto', $auto);
        
        //获取配件数据
        $PJ=M("peijian");

        $jilv=$PJ->where("type_id=2")->select();
        $this->assign("jilv",$jilv);

        $kongqilv=$PJ->where("type_id=3")->select();
        $this->assign("kongqilv",$kongqilv);

        $kongtiaolv=$PJ->where("type_id=5")->select();
        $this->assign("kongtiaolv",$kongtiaolv);

        $huohuasai=$PJ->where("type_id=14")->select();
        $this->assign("huohuasai",$huohuasai);

        $this->assign('data',$data);

        $this->display();
    }
    
    //添加修改
    public function insert_auto(){
    	$data = $_POST;
        $updataKey=0;
        $addKey=0;
        $brand=M("auto")->where("brand_id=".$data['brand_id'])->find();

        $A=M("auto_model");
        for($i=0;$i<count($data['auto_name']);$i++){
            $autoAdd[$addKey]['brand_id']=$data['brand_id'];
            $autoAdd[$addKey]['auto_name']=$data['auto_name'][$i];
            $autoAdd[$addKey]['model_pl']=$data['model_pl'];
            $autoAdd[$addKey]['capacity']=$data['capacity'][$i];
            $autoAdd[$addKey]['model_name']=$data['model_name'];
            $autoAdd[$addKey]['model_year']=$data['model_year'];
            $autoAdd[$addKey]['brand_id']=$data['brand_id'];
            $autoAdd[$addKey]['brand_logo']=$brand['brand_logo'];
            $autoAdd[$addKey]['brand_name']=$brand['brand_name'];
        }


        $firstId=$A->addAll($autoAdd);  //添加新车款

        if($firstId){
            $autoAddId=range($firstId,$firstId+count($autoAdd)-1);
        }else{
            $this->error("无新车型被添加");
        }


        $peijianTypeArr=array("jilv"=>2,"kongtiaolv"=>5,"kongqilv"=>3,"huohuasai"=>14); //key对应的配件类型
        $uIdList=$autoAddId;

        $indexKey=0;
        for($i=0;$i<count($uIdList);$i++){
            for($j=0;$j<count($_POST['jilv']);$j++){
                $indexData[$indexKey]['model_id']=$uIdList[$i];
                $indexData[$indexKey]['type']=$peijianTypeArr["jilv"];
                $indexData[$indexKey++]['pj_id']=$_POST["jilv"][$j];

            }

            for($j=0;$j<count($_POST['kongtiaolv']);$j++){
                $indexData[$indexKey]['model_id']=$uIdList[$i];
                $indexData[$indexKey]['type']=$peijianTypeArr["kongtiaolv"];
                $indexData[$indexKey++]['pj_id']=$_POST["kongtiaolv"][$j];
            }

            for($j=0;$j<count($_POST['kongqilv']);$j++){
                $indexData[$indexKey]['model_id']=$uIdList[$i];
                $indexData[$indexKey]['type']=$peijianTypeArr["kongqilv"];
                $indexData[$indexKey++]['pj_id']=$_POST["kongqilv"][$j];
            }

            for($j=0;$j<count($_POST['huohuasai']);$j++){
                $indexData[$indexKey]['model_id']=$uIdList[$i];
                $indexData[$indexKey]['type']=$peijianTypeArr["huohuasai"];
                $indexData[$indexKey++]['pj_id']=$_POST["huohuasai"][$j];
            }
        }

        $index=M("jl");
        $res=$index->addAll($indexData);
        if($res){
            $this->success("添加成功");
        }else{
            $this->error("添加失败");
        }
    }


    public function update_auto(){
        $data = $_POST;
        $updataKey=0;
        $addKey=0;

        $A=M("auto_model");
        for($i=0;$i<count($data['auto_name']);$i++){
            if($data['model_id'][$i]>0){
                $autoUpdata[$updataKey]['brand_id']=$data['brand_id'];
                $autoUpdata[$updataKey]['model_id']=$data['model_id'][$i];
                $autoUpdata[$updataKey]['auto_name']=$data['auto_name'][$i];
                $autoUpdata[$updataKey]['model_pl']=$data['model_pl'];
                $autoUpdata[$updataKey]['capacity']=$data['capacity'][$i];
                $autoUpdata[$updataKey]['model_name']=$data['model_name'];
                $autoUpdata[$updataKey]['model_year']=$data['model_year'];
                $autoUpdata[$updataKey]['res']=$A->save($autoUpdata[$updataKey]);   //更新车型
                $uIdList.=($uIdList==""?"":",").$data['model_id'][$i]; //原车型匹配配件ID列表
                $updataKey++;
            }else{
                $autoAdd[$addKey]['brand_id']=$data['brand_id'];
                $autoAdd[$addKey]['auto_name']=$data['auto_name'][$i];
                $autoAdd[$addKey]['model_pl']=$data['model_pl'];
                $autoAdd[$addKey]['capacity']=$data['capacity'][$i];
                $autoAdd[$addKey]['model_name']=$data['model_name'];
                $autoAdd[$addKey]['model_year']=$data['model_year'];
                $autoAdd[$addKey]['brand_id']=$data['brand_id'];
                $autoAdd[$addKey]['brand_logo']=$data['brand_logo'];
                $autoAdd[$addKey]['brand_name']=$data['brand_name'];
            }
        }

        $index=M("jl");
        $mapDel['model_id']=array("in",$uIdList);
        $res=$index->where($mapDel)->delete();    //删除原先配件索引

        //删除原数据成功
        if($res!==false){
            $firstId=$A->addAll($autoAdd);  //添加新车款
            if($firstId){
                $autoAddId=range($firstId,$firstId+count($autoAdd)-1);
            }

            $uIdList.=(count($autoAddId)>0?",":"").implode(",",$autoAddId); //新旧车款ID组合

            $peijianTypeArr=array("jilv"=>2,"kongtiaolv"=>5,"kongqilv"=>3,"huohuasai"=>14); //key对应的配件类型
            $uIdList=explode(",",$uIdList);

            $indexKey=0;
            for($i=0;$i<count($uIdList);$i++){
                for($j=0;$j<count($_POST['jilv']);$j++){
                    $indexData[$indexKey]['model_id']=$uIdList[$i];
                    $indexData[$indexKey]['type']=$peijianTypeArr["jilv"];
                    $indexData[$indexKey++]['pj_id']=$_POST["jilv"][$j];

                }

                for($j=0;$j<count($_POST['kongtiaolv']);$j++){
                    $indexData[$indexKey]['model_id']=$uIdList[$i];
                    $indexData[$indexKey]['type']=$peijianTypeArr["kongtiaolv"];
                    $indexData[$indexKey++]['pj_id']=$_POST["kongtiaolv"][$j];
                }

                for($j=0;$j<count($_POST['kongqilv']);$j++){
                    $indexData[$indexKey]['model_id']=$uIdList[$i];
                    $indexData[$indexKey]['type']=$peijianTypeArr["kongqilv"];
                    $indexData[$indexKey++]['pj_id']=$_POST["kongqilv"][$j];
                }

                for($j=0;$j<count($_POST['huohuasai']);$j++){
                    $indexData[$indexKey]['model_id']=$uIdList[$i];
                    $indexData[$indexKey]['type']=$peijianTypeArr["huohuasai"];
                    $indexData[$indexKey++]['pj_id']=$_POST["huohuasai"][$j];
                }
            }

            $res=$index->addAll($indexData);
            if($res){
                $this->success("修改成功");
            }else{
                $this->error("修改失败");
            }

        }
        exit;
    }
    
}

?>